/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Set of static methods for properties management 
 * 
 * @author Laurent Guerin
 * @since 1.1.1
 * 
 */
public class PropUtil 
{

    //-----------------------------------------------------------------------------------------
    /**
     * Tries to load properties from the ClassPath ( using the ClassLoader )
     * The properties file can be located in a folder or a JAR file accessible via the CLASSPATH
     * 
     * @param sPropertiesFileName the file name to be loaded
     * @return the properties loaded or null if the file cannot be found
     */
	public static Properties loadFromClassPath ( String sPropertiesFileName )
    {
    	Properties prop = null ;
//        Thread thread = Thread.currentThread() ;
//        if ( thread != null )
//        {
//            ClassLoader cl = thread.getContextClassLoader() ;
//            if ( cl != null )
//            {
//                InputStream is = cl.getResourceAsStream(sPropertiesFileName);
//                if ( is != null )
//                {
//                    prop = loadFromInputStream(is);
//                }
//            }
//        }
        
        InputStream is = ClassLoaderUtil.getResourceAsStream(sPropertiesFileName);
        if ( is != null )
        {
            prop = loadFromInputStream(is);
        }
        return prop ;
    }
    
    //-----------------------------------------------------------------------------------------
    /**
     * Tries to load properties from the file system using the given file name
     * 
     * @param sPropertiesFileName the file name to be loaded
     * @return the properties loaded or null if the file cannot be found
     */
	public static Properties loadFromFileSystem ( String sPropertiesFileName )
    {
        Properties prop = null;
        FileInputStream is = null;
//        try
//        {
//            in = new FileInputStream(sPropertiesFileName);
//            prop = new Properties();
//            prop.load(in);
//            if ( in != null )
//            {
//                prop = loadFromInputStream(in);
//            }
//        } catch (IOException ex)
//        {
//            log.error("loadProperties() : IOException : " + ex.getMessage());
//        } finally
//        {
//            if (in != null)
//            	StreamUtil.closeInputStream(in);
//        }
        try {
			is = new FileInputStream(sPropertiesFileName);
		} catch (FileNotFoundException e) {
			is = null;
		}
        if ( is != null )
        {
            prop = loadFromInputStream(is);
        }

        return prop;
    }
	
    //-----------------------------------------------------------------------------------------
    /**
     * Loads properties from the given InputStream
     * @param is
     * @return the properties loaded or null if an error occurs
     */
    public static Properties loadFromInputStream ( InputStream is )
    {
        if ( is != null )
        {            
            Properties prop = new Properties();
            try
            {
                prop.load(is);
            } catch (IOException ex)
            {
                prop = null;
	        } finally
	        {
                try {
                       is.close();
                } catch (IOException ex) {}
	        }
	        return prop ;
        }
        else
        {
            return null ;
        }
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Print the content of the properties on the standard output
     * 
     * @param prop the properties to print
     */
    public static void showProperties(Properties prop)
    {
        showProperties(prop, System.out);
    }

    //-----------------------------------------------------------------------------
    /**
     * Print the content of the properties on a specific output
     * 
     * @param prop the properties to print
     * @param out the output to use
     */
    public static void showProperties(Properties prop, PrintStream out)
    {
        Enumeration<Object> enumKeys = prop.keys();
        String sKey = null;
        Object obj = null;
        out.println(" ---------- ");
        while (enumKeys.hasMoreElements()) // For each key
        {
            sKey = (String) enumKeys.nextElement();
            obj = prop.get(sKey);
            if (obj != null)
            {
                out.println(" . " + sKey + " : " + obj.toString());
            }
        }
        out.println(" ---------- ");
    }
    

}
